<?php

!defined('IN_ROOT') && exit('Access Denied');
//$type 0 分站同业分类 1分站分类 2景点分类 3分站国内游 4 分站周边游 5分站出境游
class mapmodel {

    var $db;
    var $base;

    function mapmodel(&$base) {
        $this->base = $base;
        $this->db = $base->db;
    }
    function get($id) {
        return $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."map WHERE id='$id'");
    }
    function remove_by_id($sid,$type=0) {
    	$this->db->query("DELETE FROM `".DB_TABLEPRE."map` WHERE `sid` IN ($sid) and `stype`='$type'");
    }
    function delcate($sid,$ids,$type=0) {
    	$this->db->query("DELETE FROM `".DB_TABLEPRE."map` WHERE `sid`='$sid' and `cid` IN ($ids) and `stype`='$type'");
    }
    function delcateout($sid,$ids){
    	$this->db->query("DELETE FROM `".DB_TABLEPRE."mapout` WHERE `sid`='$sid' and `cid` IN ($ids)");
    }
    function addcateout($sid,$ids){
    	$sql = "INSERT INTO `".DB_TABLEPRE."mapout`(`sid` ,`cid`) VALUES ";
    	foreach($ids as $v) {
            if(empty($v))continue;
            $tid = (int)$tagids[$v];
            $sql .= "('$sid','$v'),";
        }
        $sql=substr($sql,0,-1);
        return $this->db->query($sql);
    }
    function addcate($sid,$ids,$type=0,$tagids=array()){
    	$sql = "INSERT INTO `".DB_TABLEPRE."map`(`sid` ,`cid` , `stype`,`tid`) VALUES ";
    	foreach($ids as $v) {
            if(empty($v))continue;
            $tid = (int)$tagids[$v];
            $sql .= "('$sid','$v', '$type','$tid'),";
        }
        $sql=substr($sql,0,-1);
        return $this->db->query($sql);
    }
       /*含映射的分类浏览页面显示子分类*/
    function list_by_cid_pid_site_out($cid,$pid,$sid) {
        $sublist=array();
        $query=$this->db->query("select id,name,grade from ".DB_TABLEPRE."category where pid=$cid order by displayorder asc,id asc");
        $subcount=$this->db->affected_rows();
        if($subcount<=0) {
            $query=$this->db->query("select id,name,grade from ".DB_TABLEPRE."category where pid=$pid order by displayorder asc,id asc");
        }
        while($category=$this->db->fetch_array($query)) {
        	$category['mid'] = (int)$this->db->result_first("SELECT id FROM `".DB_TABLEPRE."mapout` where sid='$sid' and cid='$category[id]'");
            $sublist[]=$category;
        }
        return $sublist;
    }
    function list_by_cid_pid_site($cid,$pid,$sid,$type=0) {
        $sublist=array();
        $query=$this->db->query("select id,name,grade from ".DB_TABLEPRE."category where pid=$cid order by displayorder asc,id asc");
        $subcount=$this->db->affected_rows();
        if($subcount<=0) {
            $query=$this->db->query("select id,name,grade from ".DB_TABLEPRE."category where pid=$pid order by displayorder asc,id asc");
        }
        while($category=$this->db->fetch_array($query)) {
        	$category['mid'] = (int)$this->db->result_first("SELECT id FROM `".DB_TABLEPRE."map` where sid='$sid' and cid='$category[id]' and stype='$type'");
            $sublist[]=$category;
        }
        return $sublist;
    }
    function list_by_cid_pid_area($cid,$pid,$sid,$type=0) {
        $sublist=array();
        $query=$this->db->query("select id,name,grade,ename from ".DB_TABLEPRE."area where pid=$cid order by displayorder asc,id asc");
        $subcount=$this->db->affected_rows();
        if($subcount<=0) {
            $query=$this->db->query("select id,name,grade,ename from ".DB_TABLEPRE."area where pid=$pid order by displayorder asc,id asc");
        }
        while($category=$this->db->fetch_array($query)) {
        	$category['mid'] = (int)$this->db->result_first("SELECT id FROM `".DB_TABLEPRE."map` where sid='$sid' and cid='$category[id]' and stype='$type'");
            $sublist[]=$category;
        }
        return $sublist;
    }
	function check_has_map($sid,$cid,$type=0) {
		return (int)$this->db->result_first("SELECT id FROM `".DB_TABLEPRE."map` where sid='$sid' and cid='$cid' and stype='$type'");
	}
	function check_has_map_out($sid,$cid) {
		return (int)$this->db->result_first("SELECT id FROM `".DB_TABLEPRE."mapout` where sid='$sid' and cid='$cid'");
	}
	 /*分类显示页面分类导航*/
    function get_navigation($cid=0,$contain=false) {
        $navlist=array();
        do {
            $category=$this->base->category[$cid];
            if($category) {
                $cid=$category['pid'];
                $navlist[]=$category;
            }
        }while($category && $cid);
        $navlist=array_reverse($navlist);
        !$contain && array_pop($navlist); //是否需要本分类
        return $navlist;
    }
    function get_navigation_area($cid=0,$contain=false) {
        $navlist=array();
        do {
            $category=$this->base->area[$cid];
            if($category) {
                $cid=$category['pid'];
                $navlist[]=$category;
            }
        }while($category && $cid);
        $navlist=array_reverse($navlist);
        !$contain && array_pop($navlist); //是否需要本分类
        return $navlist;
    }
}
?>
